package cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.matter;

import android.content.Intent;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.matter.MatterApi;
import cn.shyouchi.wuguanjia.android.common.Constant;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.birthday.BirthdayNotice;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.casual.CasualNotice;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.casual.ShowCasualActivity;
import cn.shyouchi.wuguanjia.android.ui.home.security.thread.message.simplenotice.StaffBirthdayActivity;
import cn.shyouchi.wuguanjia.android.ui.meetting.feedback.FeedbackMeetDetailActivity;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2017/3/16 at 15:20
 * Email: 472279981@qq.com
 * Des:
 */

public class MatterNoticeAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    public MatterNoticeAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constant.MATTER_TYPE_BIRTHDAY, R.layout.item_matter_birthday_notice_layout);
        addItemType(Constant.MATTER_TYPE_MEET_APPLY, R.layout.item_meet_apply_notice_layout);
        addItemType(Constant.MATTER_TYPE_MEET_ERROR, R.layout.item_matter_error_notice_layout);
    }

    @Override
    protected void convert(BaseViewHolder holder, final MultiItemEntity notice) {


        switch (notice.getItemType()) {
            case Constant.MATTER_TYPE_BIRTHDAY:
                handleData(holder, (BirthdayNotice) notice);
                break;

            case Constant.MATTER_TYPE_MEET_APPLY:
                handleData(holder, (MatterNotice) notice);
                break;
            case Constant.MATTER_TYPE_MEET_ERROR:
                handleData(holder, (CasualNotice) notice);
                break;
        }


        holder.getView(R.id.item_action).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (notice.getItemType() == Constant.MATTER_TYPE_BIRTHDAY) {
                    final BirthdayNotice birthdayNotice = (BirthdayNotice) notice;
                    new MatterApi().getBirthdayDetail(String.valueOf(birthdayNotice.getId()))
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new ProgressSubscriber<BaseRequestResult<MatterBirthday>>(mContext) {
                                @Override
                                public void onNext(BaseRequestResult<MatterBirthday> matterMeetBaseRequestResult) {
                                    mContext.startActivity(new Intent(mContext, StaffBirthdayActivity.class)
                                            .putExtra("noticeId", birthdayNotice.getId())
                                            .putExtra("birthday", matterMeetBaseRequestResult.getResultMessage()));
                                }
                            });


                } else if (notice.getItemType() == Constant.MATTER_TYPE_MEET_ERROR) {
                    CasualNotice casualNotice = (CasualNotice) notice;

                    mContext.startActivity(new Intent(mContext, ShowCasualActivity.class).putExtra("error", casualNotice));


                } else {
                    MatterNotice meet = (MatterNotice) notice;
                    if (meet.getCheckstatus() == 2) {
                        new MatterApi().getMeetDetail(String.valueOf(meet.getId()))
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new ProgressSubscriber<BaseRequestResult<MatterMeet>>(mContext) {
                                    @Override
                                    public void onNext(BaseRequestResult<MatterMeet> requestResult) {

                                        mContext.startActivity(new Intent(mContext, AuditMeetActivity.class)
                                                .putExtra("meet", requestResult.getResultMessage()));

//                                        mContext.startActivity(new Intent(mContext, FeedbackMeetDetailActivity.class)
//                                                .putExtra("src", 1)
//                                                .putExtra("room", requestResult.getResultMessage()));
                                    }
                                });


                    } else {
                        new MatterApi().getMeetDetail(String.valueOf(meet.getId()))
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new ProgressSubscriber<BaseRequestResult<MatterMeet>>(mContext) {
                                    @Override
                                    public void onNext(BaseRequestResult<MatterMeet> requestResult) {
                                        mContext.startActivity(new Intent(mContext, FeedbackMeetDetailActivity.class)
                                                .putExtra("src", Constant.MATTER_TYPE_BIRTHDAY)
                                                .putExtra("room", requestResult.getResultMessage()));
                                    }
                                });
                    }

                }
            }
        });
    }


    private void handleData(BaseViewHolder holder, MatterNotice notice) {
        holder.setText(R.id.tv_meet_name, notice.getName());
        holder.setText(R.id.tv_msg_type_name, notice.getUsername());
        holder.setText(R.id.tv_msg_time, notice.getCreated_at());
        holder.setText(R.id.tv_meet_time, String.format("%s - %s", notice.getStart(), notice.getEnd().substring(11)));
        holder.setText(R.id.tv_meet_holder, notice.getRoom_name());
        holder.setText(R.id.tv_meet_rom, notice.getCheckstatus_name());
    }


    private void handleData(BaseViewHolder holder, BirthdayNotice notice) {
        holder.setText(R.id.tv_msg_time, notice.getCreated_at());
        holder.setText(R.id.tv_content, notice.getText());
//        holder.setText(R.id.tv_msg_time, notice.getCreated_at());
//        holder.setText(R.id.tv_meet_time, String.format("%s - %s", notice.getStart(), notice.getEnd().substring(11)));
//        holder.setText(R.id.tv_meet_holder, notice.getRoom_name());
//        holder.setText(R.id.tv_meet_rom, notice.getCheckstatus_name());
    }

    private void handleData(BaseViewHolder holder, CasualNotice notice) {
        holder.setText(R.id.tv_msg_time, notice.getCreated_at());
        holder.setText(R.id.tv_msg_type_name, notice.getTitle());
        holder.setText(R.id.tv_content, notice.getText());
//        holder.setText(R.id.tv_msg_time, notice.getCreated_at());
//        holder.setText(R.id.tv_meet_time, String.format("%s - %s", notice.getStart(), notice.getEnd().substring(11)));
//        holder.setText(R.id.tv_meet_holder, notice.getRoom_name());
//        holder.setText(R.id.tv_meet_rom, notice.getCheckstatus_name());
    }


}
